package com.qzsoft.zhjd.machine.modules.machinemaintain.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.machine.modules.machinemaintain.dto.MachineMaintainDto;
import com.qzsoft.zhjd.machine.modules.machinemaintain.entity.MachineMaintainEntity;
import com.qzsoft.zhjd.machine.modules.machinemaintain.service.MachineMaintainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HuXiuMing
 * @date 2021/10/26 10:41
 */

@RestController
@RequestMapping("/api/machineMaintain")
public class MachineMaintainController {

    @Autowired
    private MachineMaintainService machineMaintainService;

    /**
     * @Title page
     * @Description 获取保养分页列表
     * @param params 查询条件
     * @return 保养记录分页列表
     */
    @RequiresPermissions("api:machineMaintain:list")
    @GetMapping("/page")
    public Result<IPage<MachineMaintainDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MachineMaintainDto> page = machineMaintainService.pageDto(params);
        return ResultUtils.ok(page);
    }
    

    /**
     * @Title page
     * @Description 获取保养分页列表
     * @param params 查询条件
     * @return 保养记录分页列表
     */
    @GetMapping("/page/{machieId}")
    public Result<IPage<MachineMaintainEntity>> page(@PathVariable("machieId") String machieId, @RequestParam Map<String, Object> params) {
        if(null == params) {
            params = new HashMap<String, Object>();
        }
        params.put("machieId", machieId);
        IPage<MachineMaintainEntity> page = machineMaintainService.page(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title find
     * @Description 查询保养信息
     * @param id 保养主键
     * @return 查询结果
     */
    @RequiresPermissions("api:machineMaintain:edit")
    @GetMapping("/find/{id}")
    public Result<MachineMaintainEntity> find(@PathVariable("id") String id) {
        MachineMaintainEntity entity = machineMaintainService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加保养信息
     * @param entity 保养信息
     * @return 添加结果
     */
    @RequiresPermissions("api:machineMaintain:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MachineMaintainEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        machineMaintainService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改保养信息
     * @param entity 保养信息
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody MachineMaintainEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        machineMaintainService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除保养信息
     * @param id 保养信息主键
     * @return 删除结果
     */
    @RequiresPermissions("api:machineMaintain:remove")
    @PostMapping("/delete/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> delete(@PathVariable("id") String id) {
        machineMaintainService.removeById(id);
        return ResultUtils.ok();
    }

}
